package com.dao.impl;

import com.dao.ChartRedisDao;
import com.dao.RedisGeneratorDao;
import com.entity.Chart;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作redis的 dao
 */
@Repository(value = "chartRedisDao")
public class ChartRedisDaoImpl extends RedisGeneratorDao implements ChartRedisDao {

    /**
     * 添加对象
     */
    @Override
    public boolean add(final Chart chart) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(chart.getTypeName());
                byte[] name = serializer.serialize(chart.getName());
                return connection.setNX(key, name);
            }
        });
        return result;
    }

    /**
     * 添加集合
     */
    @Override
    public boolean add(final List<Chart> list) {
        Assert.notEmpty(list);
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                for (Chart chart : list) {
//                    byte[] key  = serializer.serialize(chart.getPid());
//                    byte[] name = serializer.serialize(chart.getPname());
//                    byte[] key  = serializer.serialize(chart.getPid());
//                    byte[] key  = serializer.serialize(chart.getPid());
                    //connection.setNX(key, name);
                }
                return true;
            }
        }, false, true);
        return result;
    }

    /**
     * 删除对象 ,依赖key
     */
    @Override
    public void delete(String key) {
        List<String> list = new ArrayList<String>();
        list.add(key);
        delete(list);
    }

    /**
     * 删除集合 ,依赖key集合
     */
    public void delete(List keys) {
        redisTemplate.delete(keys);
    }

    /**
     * 修改对象
     */
    public boolean update(final Chart chart) {
//        String key = chart.getPid();
//        if (get(key) == null) {
//            throw new NullPointerException("数据行不存在, key = " + key);
//        }
//        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
//            public Boolean doInRedis(RedisConnection connection)
//                    throws DataAccessException {
//                RedisSerializer<String> serializer = getRedisSerializer();
//                byte[] key  = serializer.serialize(chart.getPid());
//                byte[] name = serializer.serialize(chart.getPname());
//                connection.set(key, name);
//                return true;
//            }
//        });
        //return result;
        return false;
    }

    /**
     * 根据key获取对象
     */
    @Override
    public Chart get(final String keyId) {
        Chart result = redisTemplate.execute(new RedisCallback<Chart>() {
            public Chart doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(keyId);
                byte[] value = connection.get(key);
                if (value == null) {
                    return null;
                }
                String nickname = serializer.deserialize(value);
                // return new Chart(keyId, nickname);
                return null;
            }
        });
        return result;
    }

    @Override
    public List<Chart> getCharts(String typeName) {
        List<Chart> charts = redisTemplate.execute(new RedisCallback<List<Chart>>() {
            public  List<Chart> doInRedis(RedisConnection connection)
                    throws DataAccessException {
                GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(typeName);
                byte[] value = connection.get(key);
                return (List<Chart>)jackson2JsonRedisSerializer.deserialize(value);
            }
        },true,false);
        return charts;
    }

    public boolean pushCharts(List<Chart> charts, String typeName) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();//利用jsckson提供的 json转换器 将key和value 序列化成json

                RedisSerializer<String> serializer = getRedisSerializer();//默认的序列化器
                byte[] key = serializer.serialize(typeName);//key使用默认序列化器 因为可以就是字符串 memberTop3byMcost
                byte[] name = jackson2JsonRedisSerializer.serialize(charts);//list 需要使用json序列化器

                connection.set(key,name);
               // connection.pExpire(key, 20000);
                //20秒redis中过期

                return true;
            }
        },true,false);
        return result;
    }
}